﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using imaginecup.util;
using Imaginecup.sprite;
using Imaginecup.gameState.temp;
using Imaginecup.util.collision;
using Imaginecup.util;
using Imaginecup.factory;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.util.resourceMan;
using Imaginecup.gameState.btnStateUtil;

namespace Imaginecup.gameState.startBtnState
{
    public class StartBtnState : GameState , ISelectBtnState
    {
        private ContentManager m_MenuContent;     

        private const int MAX_MENU = 3;

        private BtnInfo[] m_MenuButton;
        private ISprite[] m_BtnSprite;
        private DrawBtnInfo[] m_DrawBtnInfo;

        private BtnUtil m_BtnUtil;

        private EntityInfo[] m_LightInfo;
        private ISprite m_LightSprite;
        private ISprite m_BackSprite;

        private static StartBtnState m_StartBtnState = null;

        private StartBtnState()
        {
            m_MenuContent = new ContentManager(m_Game.Services);
            m_MenuContent.RootDirectory = "Content";

            m_BtnUtil = new BtnUtil();
            m_BtnUtil.SetSelectBtnState(this);
        }



        public static StartBtnState GetStartBtnState()
        {
            if (m_StartBtnState == null)
            {
                m_StartBtnState = new StartBtnState();
            }
            return m_StartBtnState;
        }


        /************************************************************************/
        /* 초기화 되는 값(메뉴인덱스)                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            Vector2 ButtonSize = new Vector2(395, 425);

            m_MenuButton = new BtnInfo[MAX_MENU]{
                new BtnInfo(new EntityInfo( 0 , 377 , 87 , 89 )),
                new BtnInfo(new EntityInfo( new Vector2(19 , 23) , ButtonSize )),
                new BtnInfo(new EntityInfo( new Vector2(394 , 23) , ButtonSize ) , false)
            };

            m_BtnSprite = new ISprite[MAX_MENU];
            m_DrawBtnInfo = new DrawBtnInfo[MAX_MENU];

            for (int i = 0; i < MAX_MENU; i++)
            {
                m_DrawBtnInfo[i] = new DrawBtnInfo();
                m_DrawBtnInfo[i].SetBtnInfo(m_MenuButton[i]);
            }


            m_LightInfo = new EntityInfo[2]{
                new EntityInfo( 92, 0, 280 , 276),
                new EntityInfo( 457, 0, 280 , 276)
            };
        }


        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {

            ResourceMan.GetResourceMan().SetTextureFrame("StoryBtn", m_MenuContent.Load<Texture2D>("start_Menu\\mode_story_but"));
            ResourceMan.GetResourceMan().SetTextureFrame("CrazyBtn", m_MenuContent.Load<Texture2D>("start_Menu\\mode_crazy_but"));
            ResourceMan.GetResourceMan().SetTextureFrame("BackBtn", m_MenuContent.Load<Texture2D>("start_Menu\\mode_back_but"));
            ResourceMan.GetResourceMan().SetTextureFrame("Light", m_MenuContent.Load<Texture2D>("start_Menu\\mode_light"));
            ResourceMan.GetResourceMan().SetTextureFrame("Back", m_MenuContent.Load<Texture2D>("start_Menu\\mode_back"));

            NormalSprite normalSprite = new NormalSprite();
            normalSprite.SetTextureName("Back");
            m_BackSprite = normalSprite;

            String[] btnNameList = new String[]{
                "BackBtn",
                "StoryBtn",
                "CrazyBtn"
            };

            for( int i = 0 ; i < btnNameList.Length ; i++ )
            {
                ChangeSourceSizeSprite sprite = new ChangeSourceSizeSprite();
                sprite.SetTextureName(btnNameList[i]);
                sprite.SetSourceInfoClass(m_DrawBtnInfo[i]);

                m_BtnSprite[i] = sprite;
            }

            normalSprite = new NormalSprite();
            normalSprite.SetTextureName("Light");

            m_LightSprite = normalSprite;


            SoundMan soundMan = SoundMan.GetSoundMan();
            if (!soundMan.IsContainSong("TitleBGM"))
            {
                soundMan.LoadSong("TitleBGM", "sound\\Title");
                soundMan.PlayBackSound("TitleBGM");
            }
        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            for (int i = 0; i < MAX_MENU; i++)
            {
                m_BtnSprite[i].Release();
                m_DrawBtnInfo[i] = null;
            }
            m_BtnSprite = null;
            m_DrawBtnInfo = null;
            m_MenuButton = null;
            m_LightInfo = null;

            m_BackSprite.Release();
            m_LightSprite.Release();      
            m_MenuContent.Unload();

            ResourceMan.GetResourceMan().Release();
          //  SoundMan.GetSoundMan().ReleaseSound();
        }



        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            m_BtnUtil.Update(gameTime);
            base.Update(gameTime);
        }

        

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();

            m_BackSprite.Draw(sb, new EntityInfo(0, 0, 800, 480));
            m_BtnUtil.Draw(sb);

            for (int i = 0; i < m_LightInfo.Length; i++ )
            {
                if( m_MenuButton[i + 1].GetEnable() )
                    m_LightSprite.Draw(sb, m_LightInfo[i]);
            }

            base.Draw(gameTime);
        }


        public void StateChange(int menuIndex)
        {
            switch (menuIndex)
            {
                case 0:
                    ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_TitleMenu));
                    return;
                case 1:
                    ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Intro));
                    return;
                case 2:
                    return;
              
            }
        }

        public int GetMaxMenu()
        {
            return MAX_MENU;
        }

        public BtnInfo[] GetBtnInfoArray()
        {
            return m_MenuButton;
        }

        public ISprite[] GetSpriteArray()
        {
            return m_BtnSprite;
        }

        public DrawBtnInfo[] GetDrawBtnArray()
        {
            return m_DrawBtnInfo;
        }
    }
}
